% Obtain expected seller surplus when entering with Fv^-1(CDFv0) corresponding to
% given competing sellers entry threshold Fv^-1(CDFv0bar) and bidder optimal response with nstar.
% Corresponds to equation 12 in the paper.
% ---------------------------------------------- %
function[Pis] = Piseller(screeningvalue,CDFv0bar,CDFv0,cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,PAR,lambdastar)

    if PAR.generalized==0
        ds  = makedist(PAR.dist,thetas(1),thetas(2));
        CDFs = @(x) cdf(ds,x);
    else %Generalized gaussian
        CDFs = @(x) ggd_cdf(x,thetas);
    end

    v0bar = fminbnd(@(x) (CDFs(x)-CDFv0bar)^2,thetas(1)-5,thetas(1)+5);
    v0    = fminbnd(@(x) (CDFs(x)-CDFv0)^2,thetas(1)-5,thetas(1)+5);
             
    % Main case: calibrate v0* on optimal lambdastar
    if nargin == 15 
       lambdastar  = lambdastarfunc(screeningvalue,v0bar,cP,pibmat,pib0vec,pdfmat,v0vec,zeroreserve,PAR);
    end
    
    % Otherwise : Don't calibrate v0* on optimal lambdastar, keep fixed at provided value
    if lambdastar == 0
        Pis = 0;
    else
        Pisgrid     = zeros(PAR.maxn,1);
        
        probnumberbidders = pdf('pois',1:PAR.maxn,lambdastar).*(1-PAR.sharenobidders); 
        probnobidders = pdf('pois',0,lambdastar)*(1-PAR.sharenobidders);
        
        if zeroreserve == 0
            pisfunc  = @(n) interp1(v0vec,pismat(n,:),v0,'linear');
        else
            pisfunc = @(n) interp1(v0vec,pis0mat(n,:),v0,'linear');    
        end

        for i = 1:PAR.maxn
            Pisgrid(i)   = pisfunc(i)*probnumberbidders(i);
        end
        Pis   = sum(Pisgrid)/(sum(probnumberbidders)+(PAR.sharenobidders)+probnobidders) - cL - (1-zeroreserve)*cR;
    end
      
end
